//  ========== 
//  = Pie = 
//  ========== 
/*
 * 创建对象         var pie = Pie.createNew();
 * 设置labelid        pie.Labelid("a");
 * 创建SVG            pie.CreateSvg();
 * 绘制                   pie.Draw();
 * 
 * 未来：
 *      ===;
 * */
! function() {
	var Pie = Object.create(Object);
	Pie = {
		"CreateNew": function() {
			return FCreateNew();
		}
	}

	function FCreateNew() {
		var pie = Object.create(Pie);
		/*属性*/
		pie = {
			"_labelid": undefined,
			"_width": undefined,
			"_data": undefined,
			"_switch_legend": undefined,
			"_switch_pie_text": undefined,
			"_innerRadius": undefined,
			"_outerRadius": undefined,
			"_labelRadius": undefined,
			"_colorScale": undefined,
			"_sequence": undefined,
			"ValueToProperty": function(value, name) {
				this[name] = value;
				return this;
			}
		}
		Object.defineProperties(pie, {
			"Labelid": {
				"value": function(v) {
					this.ValueToProperty(v, "_labelid")
					return this;
				}
			},
			"Width": {
				"value": function(v) {
					this.ValueToProperty(v, "_width");
					return this;
				}
			},
			"Data": {
				"value": function(v) {
					this.ValueToProperty(v, "_data");
					return this;
				}
			},
			"Switch_legend": {
				"value": function(v) {
					this.ValueToProperty(v, "_switch_legend");
					return this;
				}
			},
			"Switch_pie_text": {
				"value": function(v) {
					this.ValueToProperty(v, "_switch_pie_text");
					return this;
				}
			},
			"InnerRadius": {
				"value": function(v) {
					this.ValueToProperty(v, "_innerRadius");
					return this;
				}
			},
			"OuterRadius": {
				"value": function(v) {
					this.ValueToProperty(v, "_outerRadius");
					return this;
				}
			},
			"LabelRadius": {
				"value": function(v) {
					this.ValueToProperty(v, "_labelRadius");
					return this;
				}
			},
			"ColorScale": {
				"value": function(v) {
					this.ValueToProperty(v, "_colorScale");
					return this;
				}
			},
			"Sequence": {
				"value": function(v) {
					this.ValueToProperty(v, "_sequence");
					return this;
				}
			},
			"CreateSvg": {
				"value": function() {
					CreateSvg_pie(this);
					return this;
				}
			},
			"Draw": {
				"value": function() {
					Detect_function_pie(pie._data) ?
						alert("Pies3D未指定data") : (Tool_pie(this), //全局工具
							DataPretreatment_pie(this), //数据预处理
							Draw_pie(this)); //绘制吧
					return this;
				}
			}
		})
		 function CreateSvg_pie(pie) {
			//createSvg之前先判断pie各属性
			DetermineProperties_pie(pie);
			pie.svg = d3.selectAll("#" + pie._svgid)[0][0] == null ? d3.select("#" + pie._labelid).append("svg").attr("id", pie._svgid).attr("class", pie._svgclass) : pie.svg;
			pie.svg
				//							.style("background", "red")
				//              .attr("preserveAspectRatio", "xMidYMid meet")
				//              .attr("viewBox", "0 0 " + pie._width + " " + pie._height + " ");
				.attr("width", pie._width)
				.attr("height", pie._height) // : pie.svg
				//              pie.svg_ceshi1_g = pie.svg.append("g").attr("id",pie._svg_ceshi1_gid);
				//              pie.svg_ceshi2_g = pie.svg.append("g").attr("id",pie._svg_ceshi2_gid);
		}
		function Draw_pie (pie) {
			//  ========== 
			//  = arc = 
			//  ========== 
			//						pie._arc = pie.svg.selectAll("." + pie._arcclass);
			pie._arc_updata = pie.svg.selectAll("." + pie._arcclass).data(pie._pie_data);
			pie._arc_enter = pie._arc_updata.enter();
			pie._arc_exit = pie._arc_updata.exit();
			pie._arc_updata
				.transition()
				.attr("id", function(d, i) {
					console.log("genxin");
					return pie._arcid + " " + i
				})
				.style("fill", function(d, i) {
					return pie._colorScale(d.data[0]);
				})
				.attr("d", function(d) {

					return pie._arcSvg(d);
				})
				.attr("transform", "translate(" + pie._px + "," + pie._py + ")")
			pie._arc_enter.append("path")
				.attr("class", pie._arcclass)
				.attr("id", function(d, i) {
					console.log("chuangjian");
					return pie._arcid + " " + i
				})
				.style("fill", function(d, i) {
					return pie._colorScale(d.data[0]);
				})
				.attr("d", function(d) {

					return pie._arcSvg(d);
				})
				.attr("transform", "translate(" + pie._px + "," + pie._py + ")")
				//							.on("click", function(d) {
				//								click_Pie(d, pies, this);
				//							})
				//							.on("mouseover", function(d) {
				//
				//								mouseover_pies(d, pies);
				//								/* 鼠标移动时，更改样式 left 和 top 来改变提示框的位置 */
				//
				//								//				tooltip.style("left", (d3.event.pageX) + "px")
				//								//					.style("top", (d3.event.pageY + 20) + "px");
				//							})
				//							.on("mouseout", function(d, i) {
				//								mouseout_pies(d, pies);
				//								//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
				//							});
			pie._arc_exit.remove();

			pie._line_updata = pie.svg.selectAll("." + pie._lineclass).data(pie._pie_data);
			pie._line_enter = pie._line_updata.enter();
			pie._line_exit = pie._line_updata.exit();

			pie._text_updata = pie.svg.selectAll("." + pie._linetextclass).data(pie._pie_data); //pie.svg.selectAll("."+ pie._linetextclass).data(pie._pie_data);
			pie._text_enter = pie._text_updata.enter();
			pie._text_exit = pie._text_updata.exit();

			pie._legend_rect_updata = pie.svg.selectAll("." + pie._legend_rectclass).data(pie._data_select);
			pie._legend_rect_enter = pie._legend_rect_updata.enter();
			pie._legend_rect_exit = pie._legend_rect_updata.exit();

			pie._legend_text_updata = pie.svg.selectAll("." + pie._legend_textclass).data(pie._data_select); //pie.svg.selectAll("."+ pie._legend_text_class).data(pie._pie_data);
			pie._legend_text_enter = pie._legend_text_updata.enter();
			pie._legend_text_exit = pie._legend_text_updata.exit();

			pie._button_updata = pie.svg.selectAll("." + pie._buttonclass).data(pie._data_button);
			pie._button_enter = pie._button_updata.enter();
			pie._button_exit = pie._button_updata.exit();

			pie._switch_pie_text ? show_pie_text(pie) : hidden_pie_text(pie);
			pie._switch_legend ? show_legend(pie) : hidden_legend(pie);

			/**
			 * 隐藏标签
			 */
			function hidden_pie_text(pie) {
				pie._line_updata.remove();
				pie._text_updata.remove();
			}

			/**
			 * 展现标签
			 */
			function show_pie_text(pie) {
				//  ========== 
				//  = line = 
				//  ========== 
				//						pie._line = pie.svg.selectAll("." + pie._lineclass);

				pie._line_updata
					.transition()
					.attr("id", function(d, i) {
						return pie._lineid + "" + i;
					})
					.attr("transform", "translate(" + pie._px + "," + pie._py + ")")
					.attr("d", function(d, i) {
						var x0 = pie._arcSvg.centroid(d)[0];
						var y0 = pie._arcSvg.centroid(d)[1];
						var centroid = pie._arcSvg.centroid(d);
						var midAngle = Math.atan2(centroid[1], centroid[0]);
						var x1 = Math.cos(midAngle) * pie._labelRadius;
						var x2 = x1 > 0 ? pie._outerRadius + pie._line_length : -pie._outerRadius - pie._line_length;
						var midAngle = Math.atan2(centroid[1], centroid[0]);
						var centroid = pie._arcSvg.centroid(d);
						var centroid = pie._arcSvg.centroid(d);
						var midAngle = Math.atan2(centroid[1], centroid[0]);
						var y1 = Math.sin(midAngle) * pie._labelRadius;
						var ret = [];
						ret.push("M", x0, y0, "L", x1, y1, "L", x2, y1);
						return ret.join(" ");
					});
				pie._line_enter.append("path")
					.attr("class", pie._lineclass)
					.attr("id", function(d, i) {
						return pie._lineid + "" + i;
					})
					.attr("d", function(d, i) {
						var x0 = pie._arcSvg.centroid(d)[0];
						var y0 = pie._arcSvg.centroid(d)[1];
						var centroid = pie._arcSvg.centroid(d);
						var midAngle = Math.atan2(centroid[1], centroid[0]);
						var x1 = Math.cos(midAngle) * pie._labelRadius;
						var x2 = x1 > 0 ? pie._outerRadius + pie._line_length : -pie._outerRadius - pie._line_length;
						var midAngle = Math.atan2(centroid[1], centroid[0]);
						var centroid = pie._arcSvg.centroid(d);
						var centroid = pie._arcSvg.centroid(d);
						var midAngle = Math.atan2(centroid[1], centroid[0]);
						var y1 = Math.sin(midAngle) * pie._labelRadius;
						var ret = [];
						ret.push("M", x0, y0, "L", x1, y1, "L", x2, y1);
						return ret.join(" ");
					})
					.attr("transform", "translate(" + pie._px + "," + pie._py + ")")
					.style({
						stroke: "#1C90F3",
						fill: "none"
					});
				pie._line_exit.remove();

				//  ========== 
				//  = text = 
				//  ========== 
				//						pie._text = pie.svg.selectAll("."+ pie._textclass);

				pie._text_updata
					//							.transition()
					.attr("id", function(d, i) {
						return pie._textid + " " + i;
					})
					.attr({
						x: function(d, i) {
							centroid = pie._arcSvg.centroid(d);
							midAngle = Math.atan2(centroid[1], centroid[0]);
							x = Math.cos(midAngle) * pie._labelRadius;
							return x > 0 ? pie._outerRadius + pie._line_length : -pie._outerRadius - pie._line_length;
						},
						y: function(d, i) {
							centroid = pie._arcSvg.centroid(d);
							midAngle = Math.atan2(centroid[1], centroid[0]);
							y = Math.sin(midAngle) * pie._labelRadius;
							return y;
						},
						'text-anchor': function(d, i) {
							centroid = pie._arcSvg.centroid(d);
							midAngle = Math.atan2(centroid[1], centroid[0]);
							x = Math.cos(midAngle) * pie._labelRadius;
							return(x > 0) ? "start" : "end";
						}
					})
					.text(function(d) {
						return d.data[0];
					})
					.attr("transform", "translate(" + pie._px + "," + pie._py + ")");
				pie._text_enter.append("text")
					.attr("class", pie._linetextclass)
					.attr("id", function(d, i) {
						return pie._textid + " " + i;
					})
					.attr({
						x: function(d, i) {
							centroid = pie._arcSvg.centroid(d);
							midAngle = Math.atan2(centroid[1], centroid[0]);
							x = Math.cos(midAngle) * pie._labelRadius;
							return x > 0 ? pie._outerRadius + pie._line_length : -pie._outerRadius - pie._line_length;
						},
						y: function(d, i) {
							centroid = pie._arcSvg.centroid(d);
							midAngle = Math.atan2(centroid[1], centroid[0]);
							y = Math.sin(midAngle) * pie._labelRadius;
							return y;
						},
						'textBaseline': "middle",
						'text-anchor': function(d, i) {
							centroid = pie._arcSvg.centroid(d);
							midAngle = Math.atan2(centroid[1], centroid[0]);
							x = Math.cos(midAngle) * pie._labelRadius;
							return(x > 0) ? "start" : "end";
						}
					})
					.text(function(d) {
						return d.data[0];
					})
					.attr("transform", "translate(" + pie._px + "," + pie._py + ")");
				pie._text_exit.remove();

				//  ========== 
				//  = 优化线条 = 
				//  ========== 
				alpha = 0.5;
				spacing = 16;

				function relax() {
					again = false;
					pie._text_updata.each(function(d, i) {
						a = this;
						da = d3.select(a);
						y1 = da.attr("y");
						pie._text_updata.each(function(d, j) {
							b = this;
							// a & b are the same element and don't collide.
							if(a == b) return;
							db = d3.select(b);
							// a & b are on opposite sides of the chart and
							// don't collide
							if(da.attr("text-anchor") != db.attr("text-anchor")) return;
							// Now let's calculate the distance between
							// these elements. 
							y2 = db.attr("y");
							deltaY = y1 - y2;
							// Our spacing is greater than our specified spacing,
							// so they don't collide.
							if(Math.abs(deltaY) > spacing) {
								return
							};

							// If the labels collide, we'll push each 
							// of the two labels up and down a little bit.
							again = true;
							sign = deltaY > 0 ? 1 : -1;
							adjust = sign * alpha;
							da.attr("y", +y1 + adjust);
							db.attr("y", +y2 - adjust);
						});
					});
					// Adjust our line leaders here
					// so that they follow the labels. 
					if(again) {
						labelElements = pie._text_updata[0];

						pie._line_updata.attr(
								"d",
								function(d, i) {
									var x0 = pie._arcSvg.centroid(d)[0];
									var y0 = pie._arcSvg.centroid(d)[1];
									var centroid = pie._arcSvg.centroid(d);
									var midAngle = Math.atan2(centroid[1], centroid[0]);
									var x1 = Math.cos(midAngle) * pie._labelRadius;
									var centroid = pie._arcSvg.centroid(d);
									var x2 = x1 > 0 ? pie._outerRadius + pie._line_length : -pie._outerRadius - pie._line_length;

									var y1 = labelForLine = d3.select(labelElements[i]).attr("y");
									var ret = [];
									ret.push("M", x0, y0, "L", x1, y1, "L", x2, y1);
									//											console.log(ret.join(" "))
									return ret.join(" ");
								})
							//.attr("y2",function(d,i) {
							//          labelForLine = d3.select(labelElements[i]);
							//        return labelForLine.attr("y");
							//  });
						setTimeout(relax, 0)
					}
				}

				relax();
			}
		}

		/**
		 * 隐藏legend
		 */
		function hidden_legend(pie) {
			pie._button_updata.remove();
			pie._legend_rect_updata.remove();
			pie._legend_text_updata.remove();
		}

		/**
		 * 展现legend
		 */
		function show_legend(pie) {
			//pie._switch = true;

			pie._button_updata
				.attr("id", function(d, i) {
					return pie._buttonid + " " + i;
				})
				.attr({
					"transform": function(d, i) {
						var a = pie._height + 10,
							b = pie._height + 60;
						return i == 0 ? "translate(" + a + ",10)" : "translate(" + b + ",10)";
					},
					"id": function(d, i) {
						return pie._buttonid + " " + i;
					},
					"d": pie.symbol,
					"fill": function(d) {
						return d.off ? "blue" : "none";
					}

				})
				//								.attr("transform", "translate(" + pie._height + ",10)")
			pie._button_enter
				.append("path")
				.attr("id", function(d, i) {
					return pie._buttonid + " " + i;
				})
				.attr("class", pie._buttonclass)
				.attr({
					"transform": function(d, i) {
						var a = pie._height + 10,
							b = pie._height + 60;
						return i == 0 ? "translate(" + a + ",10)" : "translate(" + b + ",10)";
					},
					"id": function(d, i) {
						return pie._buttonid + " " + i;
					},
					"d": pie.symbol,
					"fill": function(d) {
						return d.off ? "blue" : "none";
					}

				})
				//								.attr("transform", "translate(" + pie._height + ",10)")
				.on("click", function(d, i) {
					Click_button(i, pie);
				});
			pie._button_exit.remove();

			pie._legend_rect_updata //.append("rect")
				.attr("id", function(d, i) {
					return pie._legend_rectid + " " + i;
				})
				.attr("x", 0)
				.attr("y", function(d, i) {
					return i * (20 + pie._height / 11 - 20) + pie._height / 11 - 20;
				})
				.attr("width", 20)
				.attr("height", 20)
				.style("fill", function(d, i) {
					return pie._colorScale(d.data[0]); //pie._colorScale(d[0]);
				});
			pie._legend_rect_enter.append("rect")
				.attr("class", pie._legend_rectclass)
				.attr("id", function(d, i) {
					return pie._legend_rectid + " " + i;
				})
				.attr("x", 0)
				.attr("y", function(d, i) {
					return i * (20 + pie._height / 11 - 20) + pie._height / 11 - 20;
				})
				.attr("width", 20)
				.attr("height", 20)
				.style("fill", function(d, i) {
					return pie._colorScale(d.data[0]);
				})
				.attr("transform", function(d, i) {
					var b = pie._height;
					return "translate(" + b + ",10)";
				})
				//				.on("click", function(d,i) {
				//					var pie = legend._BindingObject.svg.selectAll("." + legend._BindingObject._arc_class)[0][d[1]];
				//					click_Pie(d, legend._BindingObject, pie);
				//				})
				//				.on("mouseover", function(d) {
				//					mouseover_pies(d, legend);
				//					/* 鼠标移动时，更改样式 left 和 top 来改变提示框的位置 */
				//
				//					//				tooltip.style("left", (d3.event.pageX) + "px")
				//					//					.style("top", (d3.event.pageY + 20) + "px");
				//				})
				//				.on("mouseout", function(d, i) {
				//					mouseout_pies(d, legend);
				//					//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
				//				});
			pie._legend_rect_exit.remove();
			pie._legend_text_updata //.append("text")
				.attr("id", function(d, i) {
					return pie._legend_textid + " " + i;
				})
				.attr("x", 30)
				.attr("y", function(d, i) {
					return i * (20 + pie._height / 11 - 20) + pie._height / 11 - 5;
				})
				.text(function(d, i) {
					return d.data[0].substring(0, 5);
				})
				.style("font-size", "12px");
			pie._legend_text_enter.append("text")
				.attr("class", pie._legend_textclass)
				.attr("id", function(d, i) {
					return pie._legend_textid + " " + i;
				})
				.attr("x", 30)
				.attr("y", function(d, i) {
					return i * (20 + pie._height / 11 - 20) + pie._height / 11 - 5;
				})
				.text(function(d, i) {
					//					formatter:function(c){
					//
					//for(i in c){ 
					//return c.substring(0,12); 
					//} 
					//}
					return d.data[0].substring(0, 5);
				})
				.style("font-size", "12px")
				.attr("transform", function(d, i) {
					var b = pie._height;
					return "translate(" + b + ",10)";
				})
				//				.on("click", function(d) {
				//					var pie = legend._BindingObject.svg.selectAll("." + legend._BindingObject._arc_class)[0][d[2]];
				//					click_Pie(d, legend._BindingObject, pie);
				//				})
				//				.on("mouseover", function(d) {
				//					mouseover_pies(d, legend);
				//					/* 鼠标移动时，更改样式 left 和 top 来改变提示框的位置 */
				//
				//					//				tooltip.style("left", (d3.event.pageX) + "px")
				//					//					.style("top", (d3.event.pageY + 20) + "px");
				//				})
				//				.on("mouseout", function(d, i) {
				//					mouseout_pies(d, legend);
				//					//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
				//				});
			pie._legend_text_exit.remove();
		}
		Tool_pie = function(pie) {
			pie._sequence = Detect_function(pie._sequence) ? null : pie._sequence;
			pie._colorScale = Detect_function(pie._colorScale) ? d3.scale.category20() : pie._colorScale;
			console.log(pie._innerRadius, pie._outerRadius);
			pie._arcSvg = d3.svg.arc()
				.innerRadius(pie._innerRadius)
				.outerRadius(pie._outerRadius);
			pie.symbol = d3.svg.symbol()
				.type(function(d, i) {
					return i == 0 ? d3.svg.symbolTypes[4] : d3.svg.symbolTypes[5];
				})
				.size(60);
		}
		Sequence_pie = function(sequence) {
				return sequence == 1 ? function comparator(a, b) {
						return d3.ascending(parseInt(a[1]), parseInt(b[1])); //升序
					} :
					(sequence == -1 ? function comparator(a, b) {
						return d3.descending(parseInt(a[1]), parseInt(b[1])); //降序 
					} : null)
			}
			//  ========== 
			//  = 判断属性 = 
			//  ========== 
		Detect_function_pie = function(object_lh) {
			return typeof(object_lh) == "undefined";
		}
		DetermineProperties_pie = function(pie) {
			//						Detect_function_pie(pie._labelid) ?
			//							console.log("父容器id呢") :
			//							d3.select("#" + pie._labelid)[0][0] == null ?
			//							console.log("请检查id是否正确") :
			SetId_pie(pie);
			/*
			 *	定义：legend 宽度
			 * 	*：是否设为可设 暂时写死
			 * */
			pie._legend_width = 100;
			pie._text_width = 100;
			/*width限制最小为360*/
			//						pie._width = Detect_function_pie(pie._width) ? $("#" + pie._labelid).width() : pie._width;
			//						pie._switch_legend = pie._switch_legend || Detect_function_pie(pie._switch_legend) ? true : false;
			//						pie._switch_pie_text = pie._switch_pie_text || Detect_function_pie(pie._switch_pie_text) ? true : false;
			pie._pie_svg_margin = pie._switch_legend * pie._legend_width; //+ pie._switch_pie_text * 100;
			pie._height = pie._width - pie._pie_svg_margin; // >= 500 ? 500  - pie._pie_svg_margin : pie._width - pie._pie_svg_margin;
			//						pie._pie_width = 
			pie._px = pie._height / 2;
			pie._py = pie._height / 2;
			pie._innerRadius = 0;

			/**
			 * 
			 * 需求：	1.可设置外边距
			 * 		2.自适应svg宽度。
			 * 实现：	判断_outerRadius是否未定义。未定义则自适应svg。定义则输出定义值。
			 * bug: 外边距设置后则无法自适应，
			 * bug_code:	pie._outerRadius = Detect_function_pie(pie._outerRadius) ? (pie._height - pie._switch_pie_text * 100) / 2 : pie._outerRadius; 
			 * 解决方案： 	1.暂时完全自适应；
			 * 			2.pie._outerRadius设置时再次判断与svg的关系。
			 */
			//						pie._outerRadius = Detect_function_pie(pie._outerRadius) ? (pie._height - pie._switch_pie_text * 100) / 2 : pie._outerRadius; 
			//						pie._outerRadius = (pie._height - pie._switch_pie_text * 100) / 2 ; 
			pie._outerRadius = SetOuterRaduis(pie);

			function SetOuterRaduis(pie) {
				var R = (pie._height - pie._switch_pie_text * 100 * 2) / 2;
				return R //pie._outerRadius =  pie._outerRadius >= R ? R : pie._outerRadius; 
			}
			pie._labelRadius = pie._outerRadius + 5;
			pie._line_length = 10;
		}
		function DataPretreatment_pie(pie) {
			//						pie._legend1.data(pie._data)
			pie._button_select = 10;
			Button_Off(pie);

			//						legend._data_name = [];
			//						legend._data.map(function(d, i) {
			//							d[2] = i;
			//							if(d[1] != "0") {
			//								legend._data_name.push([d[0], d[2]]);
			//							}
			//						});
			//						for(var i = 0; i < legend._data.length; i++) {
			//							legend._colorScale(legend._data[i][0]);
			//						}
			//
			//						legend._data_select = legend._data_name.slice(legend._select_value * 10, legend._select_value * 10 + 10);

			pie._pie_data = d3.layout.pie()
				.sort(Sequence_pie(pie._sequence))
				.value(function(d) {
					return d[1];
				})(pie._data);
			pie._select_value = 0;
			pie._data_select = pie._pie_data.slice(pie._select_value * 10, pie._select_value * 10 + 10);
		}

		function Click_button(i, pie) {

			//  ========== 
			//  = button过渡 = 
			//  ========== 
			i == 0 ? pie._button_select += 10 : pie._button_select -= 10;

			Button_Off(pie);
			Transition_button(pie);

			//  ========== 
			//  = legend过渡 = 
			//  ========== 
			//						pie._legend1.LTransition(i);
			i == 0 ? pie._select_value += 1 : pie._select_value -= 1;
			//			i == 0 ? Transition_down(this):transition_up(this);
			pie._data_select = pie._pie_data.slice(pie._select_value * 10, pie._select_value * 10 + 10);
			pie._legend_rect_updata = pie.svg.selectAll("." + pie._legend_rectclass).data(pie._data_select);
			pie._legend_rect_enter = pie._legend_rect_updata.enter();
			pie._legend_rect_exit = pie._legend_rect_updata.exit();

			pie._legend_text_updata = pie.svg.selectAll("." + pie._legend_textclass).data(pie._data_select); //pie.svg.selectAll("."+ pie._legend_text_class).data(pie._pie_data);
			pie._legend_text_enter = pie._legend_text_updata.enter();
			pie._legend_text_exit = pie._legend_text_updata.exit();
			pie._button_updata = pie.svg.selectAll("." + pie._buttonclass).data(pie._data_button);
			pie._button_enter = pie._button_updata.enter();
			pie._button_exit = pie._button_updata.exit();
			show_legend(pie)
		}

		function Transition_button(pie) {
			pie._button_updata = pie.svg.selectAll("." + pie._buttonclass).data(pie._data_button)
				.transition()
				.duration(100)
				.style({
					"fill": function(d) {
						return d.off ? "blue" : "none";
					}
				})
		}
		function Button_Off(pie) {
			pie._data_name = [];
			pie._data.map(function(d) {
				if(d[1] != "0") {
					pie._data_name.push(d[0]);
				}
			});
			pie._button_down = pie._data_name.length - pie._button_select > 0 ? true : false;
			pie._button_up = pie._button_select - 10 > 0 ? true : false;
			pie._data_button = [{
				"off": pie._button_down
			}, {
				"off": pie._button_up
			}];
		}
		function SetId_pie(pie) {
			pie._svgid = pie._labelid + "svg";
			pie._arcid = pie._labelid + "pie_arc";
			pie._lineid = pie._labelid + "pie_line";
			pie._textid = pie._labelid + "pie_text";
			pie._buttonid = pie._labelid + "button"
			pie._legend_rectid = pie._labelid + "legend_rect";
			pie._legend_textid = pie._labelid + "legend_text";

			pie._svgclass = pie._labelid + "svg_c";
			pie._arcclass = pie._labelid + "pie_arc_c";
			pie._lineclass = pie._labelid + "pie_line_c";
			pie._linetextclass = pie._labelid + "pie_text1_c";
			pie._buttonclass = pie._labelid + "buttonc";
			pie._legend_rectclass = pie._labelid + "legend_rect_c";
			pie._legend_textclass = pie._labelid + "legend_text_c";
			//          pie._svg_ceshi1_gid = pie._labelid + "svg_ceshi1_g";
			//          pie._svg_ceshi2_gid = pie._labelid + "svg_ceshi2_g";
		}
		return pie;
	};
	this.Pie = Pie;
}();